/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webclient;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.PanelLayout;
import javax.faces.FacesException;
import javax.faces.component.html.HtmlOutputText;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version actualizarTrayectos.java
 * @version Created on 05-may-2009, 0:08:25
 * @author Admin
 */

public class actualizarTrayectos extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private HtmlOutputText outputText2 = new HtmlOutputText();

    public HtmlOutputText getOutputText2() {
        return outputText2;
    }

    public void setOutputText2(HtmlOutputText hot) {
        this.outputText2 = hot;
    }
    private PanelLayout layoutPanel2 = new PanelLayout();

    public PanelLayout getLayoutPanel2() {
        return layoutPanel2;
    }

    public void setLayoutPanel2(PanelLayout pl) {
        this.layoutPanel2 = pl;
    }

    // </editor-fold>

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public actualizarTrayectos() {
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();

        try { // Call Web Service Operation
            gestionreservas.AdministradorTrenesService service = new gestionreservas.AdministradorTrenesService();
            gestionreservas.AdministradorTrenes port = service.getAdministradorTrenesPort();
            // TODO initialize WS operation arguments here
            long fecha = System.currentTimeMillis();
            boolean borrado = false;
            // TODO process result here
            int result = port.borrarPasados(fecha, borrado);
            System.out.println("Result = "+result);
            java.util.Date date=new java.util.Date(fecha);
            String f=new String();
            f=f.concat(String.valueOf(date.getDate()));
            f=f.concat("/"+String.valueOf(date.getMonth()+1));
            f=f.concat("/"+String.valueOf(date.getYear()+1900));
            if(result>0){
                this.outputText2.setValue("Se borrarán "+result+" entradas de la base de datos de trayectos anteriores al día de hoy ("+f+")");
                this.layoutPanel2.setVisible(true);
            }
            else{
                if(result==0)
                    this.outputText2.setValue("No existen trayectos anteriores al dia de hoy ("+f+") en la base de datos");
                else
                    this.outputText2.setValue("Ocurrió algún error al intentar conectar con la base de datos");

                this.layoutPanel2.setVisible(false);
            }
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }

        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("actualizarTrayectos Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    public String button1_action() {

        try { // Call Web Service Operation
            gestionreservas.AdministradorTrenesService service = new gestionreservas.AdministradorTrenesService();
            gestionreservas.AdministradorTrenes port = service.getAdministradorTrenesPort();
            // TODO initialize WS operation arguments here
            long fecha = System.currentTimeMillis();
            // TODO process result here
            int result = port.borrarPasados(fecha, true);
            System.out.println("Result = "+result);
            this.init();
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }

        return null;
    }
    
}

